/**
 * FileName: GACurrencyUpdateHandler.js
 * Author: Abid Hussain
 * DateCreated: 24/06/2011
 */
exports.GACurrencyUpdateHandler = Core.Class.subclass({
	classname: 'GACurrencyUpdateHandler',
	initialize: function(delegate){
		this._delegate = delegate;
	},
	updateCurrency: function(purchasedCoins,purchasedBucks,offerCoins,offerBucks){
		this._purchasedCoins = purchasedCoins;
		this._purchasedBucks = purchasedBucks;
		this._offerCoins = offerCoins;
		this._offerBucks = offerBucks;
		var baseUrl = GAConfigurations.getURL()+'updateCurrencies';
		var params = this._getPostParams();
		var webFetcher = new GAWebFetcher(this,30,0);
		webFetcher.postData(baseUrl,params,null);
	},
	_getPostParams: function(){
		var params = new Object();
		params.type = "userkey";
		params.userkey = GAConfigurations.getUserId();
		params.game = GAConfigurations.getGameCode();
		params.purchasedCurrency1 = this._purchasedBucks;
		params.purchasedCurrency2 = this._purchasedCoins;
		params.offersCurrency1Awarded = this._offerBucks;
		params.offersCurrency2Awarded = this._offerCoins;
		return params;
	},
	webFetchSuccess: function(webFetcher,response){
		try{
			var serverResponse = eval("("+ response +")");
			var status = serverResponse.status;
			if(status.code != 1){
				this._delegate.currencyUpdateFailure(status.message);
			}
			else{
				console.log('GAPI:: Currencies hav been updated..');
				this._delegate.currencyUpdateSuccess();
			}
		}
		catch(exception){
			this._delegate.currencyUpdateFailure(exception);
		}
		finally{
			if(webFetcher != null){
				webFetcher.destroy();
				webFetcher = null;
			}
		}
	},
	webFetchFailure: function(webFetcher, error){
		this._delegate.currencyUpdateFailure(error);
		if(webFetcher != null){
			webFetcher.destroy();
			webFetcher = null;
		}
	},
	destroy: function(){
		this._delegate = null;
		this._purchasedCoins = null;
		this._purchasedBucks = null;
		this._offerCoins = null;
		this._offerBucks = null;
	}
});